import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/primeng';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { FormsModule, Validators, FormGroup, FormControl } from '@angular/forms';
import * as moment from 'moment';
import * as _ from 'lodash';
import { UserControlService } from './user-control.service';
import { AppPaginationComponent, PaginationModel } from "../../../widgets/pagination/pagination.component";
import { environment } from '../../../../environments/environment';
@Component({
  selector: 'app-user-control',
  templateUrl: './user-control.component.html',
  styleUrls: ['./user-control.component.scss'],
  providers: [UserControlService]
})
export class UserControlComponent implements OnInit {

  public listCount = 10;
  public ifEdit = 0;
  //编辑添加用户post数据
  public editOrAddUserPost = {
    sUsername: '',
    sRealname: '',
    sLocallogin: '',
    sManual: ''

  };
  /**
  * 用户分页列表数据
  */
  public userList = [];
  /**
   * 用户列表管理分页
   */
  public userListPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  /**
   * 查询用户分页列表post数据
   */
  public getUserPost = {
    pageIndex: 1,
    pageSize: environment.pageSize,
    sUsername: '',
    sRealname: ''
  };
  public system: any = {
    name: "",
    describe: ""
  };
  constructor(public userControlService: UserControlService) { }

  ngOnInit() {
    this.getUserList();

  }
  //用户列表管理分页
  public userPageNavigation(currentPageNum: number) {
    this.userListPageInfo.currentPageNum = currentPageNum;
    this.getUserList(this.userListPageInfo.currentPageNum,
      this.userListPageInfo.pageSize)
  }
  /**
* 改变用户列表显示记录数
* @param {pageSize}
*/
  public userpageSizeChange(pageSize: number) {
    if (pageSize !== this.userListPageInfo.pageSize) {
      this.userListPageInfo.pageSize = pageSize;
      this.getUserList();
    }
  }
  /**
   * 点击打开用户管理modal
   */
  showUserControlInfo() {
    window['$']("#add-user").modal('hide');
    window['$']("#user-control-info").modal();
  }
  /**
   * 点击打开新增修改用户modal
   */
  showEditOrAddUser(type, item?) {
    let that = this;
    that.ifEdit = type;
    if (type == 2) {
      this.editOrAddUserPost.sUsername = item.sUsername;
      this.editOrAddUserPost.sRealname = item.sRealname;
      this.editOrAddUserPost.sLocallogin = item.sLocallogin;
      this.editOrAddUserPost.sManual = item.sManual;
    } else if (type == 1) {
      this.editOrAddUserPost.sUsername = "";
      this.editOrAddUserPost.sRealname = "";
      this.editOrAddUserPost.sLocallogin = "";
      this.editOrAddUserPost.sManual = "";
    }
    window['$']("#edit-user").modal('show');
  }
  /**
   * 用户分页列表
   */
  getUserList(pageNum?, pageSize?) {
    this.getUserPost.sUsername = _.trim(this.getUserPost.sUsername);
    this.getUserPost.sRealname = _.trim(this.getUserPost.sRealname);
    this.getUserPost.pageIndex = pageNum ? pageNum : this.userListPageInfo.currentPageNum;
    this.getUserPost.pageSize = pageSize ? pageSize : this.userListPageInfo.pageSize;
    this.userControlService.getUserList(this.getUserPost).subscribe(data => {
      if (data) {
        this.userList = data.list;
        this.userListPageInfo.currentPageNum = data.pageNum;
        this.userListPageInfo.totalPages = data.pages;
        this.userListPageInfo.total = data.total;
        this.userListPageInfo.startRow = data.startRow;
        this.userListPageInfo.endRow = data.endRow;
      }
    })
  }
  /**
  * 添加用户数据
  */
  addUser() {
    let that = this;
    if (this.requireAdd()) {
      window['swal']({
        title: "提示",
        text: "是否确定保存用户信息?",
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: false,
        confirmButtonText: "确认",
        confirmButtonColor: "#DD6B55",
        cancelButtonText: "取消",
        showLoaderOnConfirm: true,
      }, function (isConfirm) {
        if (isConfirm) {
          that.userControlService.addUser(that.editOrAddUserPost).subscribe(data => {
            if (data) {
              window['$']("#edit-user").modal('hide');
              window['swal']("成功", "用户添加成功！", "success");
              that.getUserList();
            }
          })
        }
      })
    }
  }
  /**
  * 编辑用户数据
  */
  editUser() {
    let that = this;
    if (this.requireAdd()) {
      window['swal']({
        title: "提示",
        text: "是否确定保存用户信息?",
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: false,
        confirmButtonText: "确认",
        confirmButtonColor: "#DD6B55",
        cancelButtonText: "取消",
        showLoaderOnConfirm: true,
      }, function (isConfirm) {
        if (isConfirm) {
          that.userControlService.editUser(that.editOrAddUserPost).subscribe(data => {
            if (data) {
              window['$']("#edit-user").modal('hide');
              window['swal']("成功", "用户修改成功！", "success");
              that.getUserList();
            }
          })
        }
      })
    }
  }
  // 新增、编辑必填校验
  requireAdd() {
    let that = this;
    let requiredAddTip = [];
    let sUsername = _.trim(this.editOrAddUserPost.sUsername);
    let sRealname = _.trim(this.editOrAddUserPost.sRealname);
    if (sUsername == "" || sUsername == null) {
      requiredAddTip.push("账号");
    }
    if (sRealname == "" || sRealname == null) {
      requiredAddTip.push("姓名");
    }
    if (requiredAddTip.length > 0) {
      let requiredAddTips = requiredAddTip.join("、");
      window['swal']("提示", requiredAddTips + "不能为空", "warning");
      return false;
    } else {
      return true;
    }
  }
  /**
   * 删除单个用户
   */
  delUser(sUsername) {
    let that = this;
    window['swal']({
      type: "warning",
      showCancelButton: true,
      cancelButtonText: "取消",
      confirmButtonText: "确认",
      closeOnConfirm: false,
      showLoaderOnConfirm: true,
      title: "提示",
      // text: '<select><option value>--请选择--</option><option value>立项中</option><option value>待报会</option><option value>一次反馈</option><option value>二次反馈</option><option value>待受理</option><option value>已获批</option><option value>募集筹备</option><option value>发行中</option><option value>已成立</option><option value>报备中</option><option value>运作中</option><option value>已清盘</option></select>',
      text: "请确认是否删除选中的名称为" + sUsername + "的用户",
      html: true
    }, function () {
      that.userControlService.delUser(sUsername).subscribe(data => {
        if (data) {
          window['swal']("成功", "用户删除成功！", "success");
          that.getUserList();
        }
      })
    });
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.userList, [colName], ["asc"]);
    if (JSON.stringify(this.userList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.userList, [colName], ["desc"]);
    }
    this.userList = listDataAsc;
  }
}
